package com.example.administrator.waitan.find.life;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.administrator.waitan.R;
import com.example.administrator.waitan.base.BaseActivity;
import com.example.administrator.waitan.find.FindFragment;
import com.example.administrator.waitan.find.life.LifeContentActivity.LifeContentActivity;
import com.example.administrator.waitan.tools.GsonRequest;
import com.example.administrator.waitan.tools.VolleySingleton;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/1/12.
 */
public class LifeActivity extends BaseActivity {
    private RecyclerView mRecyclerView;
    private LifeRecyclerViewAdapter mAdapter;
    private RequestQueue queue;
    private List<LifeInfo> lifeInfos;
    private String url = "http://im.izhuti.cn/api/api_list.php?do=type&pid=type&tid=62&page=1&word=1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_life);
        final LifeProgressDialog dialog = new LifeProgressDialog(this,"一直在路上",R.anim.activity_in);
        dialog.show();
        mRecyclerView = (RecyclerView) findViewById(R.id.rc_life);
        GsonRequest<LifeBeans> lifeBeansGsonRequest = new GsonRequest<LifeBeans>(url, LifeBeans.class, new Response.Listener<LifeBeans>() {
            @Override
            public void onResponse(LifeBeans response) {
                mAdapter = new LifeRecyclerViewAdapter(LifeActivity.this, response.getList());
                mRecyclerView.setLayoutManager(new GridLayoutManager(LifeActivity.this, 2));
                mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
                mRecyclerView.setAdapter(mAdapter);
                mRecyclerView.setLayoutAnimation(getListAnim());
                dialog.dismiss();
                mAdapter.setMyClickListener(new MyClickListener() {
                    @Override
                    public void onClick(int position) {
                        Intent goLifeContent = new Intent(LifeActivity.this, LifeContentActivity.class);
                        goLifeContent.putExtra("position", position);
                        startActivity(goLifeContent);
                    }
                });
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        VolleySingleton.getInstance()._addRequest(lifeBeansGsonRequest);
    }
    private LayoutAnimationController getListAnim() {
        AnimationSet set = new AnimationSet(true);
        Animation animation = new AlphaAnimation(0.0f, 1.0f);
        animation.setDuration(1000);
        set.addAnimation(animation);

        animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        animation.setDuration(1000);//设置动画时间
        set.addAnimation(animation);
        LayoutAnimationController controller = new LayoutAnimationController(
                set, 0.5f);
        return controller;
    }
}
